<?php
namespace app\admin\controller;
use think\facade\View;
use think\facade\Db;
use app\BaseController;
class Common extends BaseController
{

    protected $mod,$role,$system,$nav,$menudata,$cache_model,$categorys,$module,$moduleid,$adminRules,$HrefId;
    public $aid=0;
    public $username='';
    public $admin=[];
    protected function initialize()
    {
        $this->admin=session('admin');
        //判断管理员是否登录
        if (empty($this->admin)) {
            session('referer',$this->request->url());
            if($this->request->isAjax()){
                $this->error('请先登录',myurl('admin/login/index'));
            }else{
                $this->redirect(myurl('admin/login/index'));
            }

        }
        define('CONTROLLER',strtolower($this->request->controller()));
        define('ACTION',strtolower($this->request->action()));
        //权限管理
        //当前操作权限ID
        $group_id=\think\facade\Db::name('admin')->cache('admin_id_'.$this->admin['admin_id'])->where('admin_id','=',$this->admin['admin_id'])->value('group_id');
        if( $group_id!=1){

            $this->HrefId = \think\facade\Db::name('auth_rule')->whereRaw('LOWER(`href`)=:href AND authopen=1',['href'=>CONTROLLER.'/'.ACTION])->value('id');
            //当前管理员权限
            $map['a.admin_id'] =  $this->admin['admin_id'];
            $default=config('database.default');
            $prefix = config('database.connections.'.$default.'.prefix');
            $rules=Db::table($prefix.'admin')->alias('a')->cache('rules_'.$this->admin['admin_id'])
                ->join($prefix.'auth_group ag','a.group_id = ag.id','left')
                ->where($map)
                ->value('ag.rules');
            $this->adminRules = explode(',',$rules);
            if($this->HrefId){
                if(!in_array($this->HrefId,$this->adminRules)){
                    $this->error('您无此操作权限');
                }
            }
        }
//        $this->cache_model=array('Module','AuthRule','Category','Posid','Field','System','cm');
        $this->cache_model=['System'];

        foreach($this->cache_model as $r){
            if(!cache($r)){
                savecache($r);
            }
        }

        $this->system = cache('System');
        View::assign('system',$this->system);
        View::assign('admin',$this->admin);
//        $this->categorys = cache('Category');
//        $this->module = cache('Module');
//        $this->mod = cache('Mod');
//        $this->rule = cache('AuthRule');
//        $this->cm = cache('cm');
    }
    //空操作
    public function _empty(){
        $this->error('空操作，返回上次访问页面中...');
    }

//
public function getGrabCount(){
    $more=$this->system['more'];
    $cycleList=\app\admin\model\System::getCycleList();
    if(isset($more['customer_limit_counts']) && $more['customer_limit_counts']>0 && isset($cycleList[$more['customer_limit_condition']])){
        if($more['customer_limit_condition']=='day'){
            $starttime=mktime(0,0,0,date('m'),date('d'),date('Y'));;
        }elseif($more['customer_limit_condition']=='week'){
            $starttime=mktime(0,0,0,date('m'),date('d')-date('w')+1,date('Y'));
        }else{
            $starttime=mktime(0,0,0,date('m'),1,date('Y'));
        }

        $crmGrabCount=Db::table('crm_grab')->where("`createtime` >= {$starttime} and `admin_id`={$this->admin['admin_id']}")->count('*');
        if($crmGrabCount >= $more['customer_limit_counts']){
            return ['code'=>false,'msg'=>$cycleList[$more['customer_limit_condition']].'抢客次数已用完'];
        }else{
            $shy=$more['customer_limit_counts']-$crmGrabCount;
            return ['code'=>true,'msg'=>$cycleList[$more['customer_limit_condition']].'剩余次数'.$shy];
        }
    }else{
        return ['code'=>true,'msg'=>''];
    }
}

}
